package cn.melon.mod.login;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.melon.Helper.DBHelper;
import cn.melon.base.IBaseExtractor;
import net.ucanaccess.jdbc.UcanaccessSQLException;

/**
 * @ClassName: IUserDaoImpl
 * @author corvo
 * @Description: 数据库访问实现
 */
public class IUserDaoImpl implements IUserDao {
    
    private DBHelper<UserInfo> dbHelper = new DBHelper<>();
    
    private String sqlCreate = "CREATE TABLE user ("
            + "card_num VARCHAR(10),"
            + "password VARCHAR(30),"
            + "type VARCHAR(10),"
            + "UUID VARCHAR(50))";
            
    
    private IBaseExtractor<UserInfo> extractor = new IBaseExtractor<UserInfo>() {

        @Override
        public List<UserInfo> convertToList(ResultSet rs) {
            if (rs == null) {
                return null;
            }
            List<UserInfo> infoList = new ArrayList<>();
            
            try {
                while (rs.next()) {
                    UserInfo info = new UserInfo();
                    info.setStudentNum(rs.getString("card_num"));
                    info.setPassword(rs.getString("password"));
                    info.setType(rs.getString("type"));
                    info.setUUID(rs.getString("UUID"));
                    infoList.add(info);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return infoList;
        }
    };
    
    public Boolean onCreate() {
        return dbHelper.onCreate(sqlCreate);
    }
    

    @Override
    public UserInfo getUser(String cardNum, String password) {
        String sqlQuery = "SELECT * FROM user where card_num = ? and password = ?";
        String [] paras = {cardNum, password};
        List<UserInfo> infoList = null;
        try {
            infoList = dbHelper.sqlQuery(sqlQuery, paras, extractor);
        } catch (UcanaccessSQLException e) {
            e.printStackTrace();
        }
        
        if (infoList != null && infoList.size() > 0) {
            return infoList.get(0);
        } else {
            return null;
        }
        
    }

    @Override
    public UserInfo getUser(String UUID) {
        
        String sqlQuery = "SELECT * FROM user where UUID = ?";
        String [] paras = {UUID};
        List<UserInfo> infoList = null;
        try {
            infoList = dbHelper.sqlQuery(sqlQuery, paras, extractor);
        } catch (UcanaccessSQLException e) {
            e.printStackTrace();
        }
        
        if (infoList != null && infoList.size() > 0) {
            return infoList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public Boolean modifyUser(UserInfo userInfo) {
        String sqlUpdate = "UPDATE user SET password = ?, type = ?, UUID = ? where card_num = ?";
        String[] paras = 
                {userInfo.getPassword(), userInfo.getType(), userInfo.getUUID(), userInfo.getStudentNum()};
        return dbHelper.sqlUpdate(sqlUpdate, paras);
    }
    
    // TODO 删去main函数, 修改为test
    public static void main(String[] args) {
        IUserDaoImpl daoImpl = new IUserDaoImpl();
        //System.out.println(daoImpl.onCreate());
        UserInfo info = daoImpl.getUser("213141748", "123456");
        System.out.println(info.getUUID());
        System.out.println(info.getType());
    }



}
